home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / zmdm.zoo / fileio.c < prev    next >
C/C++ Source or Header  |  1991-04-27  |  3KB  |  178 lines

  1. /*
  2.  *     File I/O (with large buffers) Module
  3.  *
  4.  *    Jwahar Bammi
  5.  *     bang:   {any internet host}!dsrgsun.ces.CWRU.edu!bammi
  6.  *     domain: bammi@dsrgsun.ces.CWRU.edu
  7.  *    GEnie:    J.Bammi
  8.  */
  9.  
  10. #include "config.h"
  11.  
  12. #include "zmdm.h"
  13. #include "common.h"
  14.  
  15. #define    O_RDONLY    1
  16. #define O_WRONLY    2
  17. #define O_APONLY    4
  18. #ifndef DYNABUF
  19. #define MBUFSIZ        (((long)BBUFSIZ)-1L)
  20. #else
  21. static long MBUFSIZ;
  22. #endif /* DYNABUF */
  23.  
  24. static unsigned char *bptr;
  25.  
  26. static long flcount = (-1L);
  27. static int bufmode;
  28.  
  29. int stfopen(name, mode)
  30. char *name, *mode;
  31. {
  32.     register int handl;
  33.  
  34.     switch(*mode)
  35.     {
  36.         case 'r':
  37.         if((handl = Fopen(name, 0)) <= 0)
  38.             return -1;
  39.         bufmode = O_RDONLY;
  40.         break;
  41.  
  42.         case 'w':
  43.         if((handl = Fcreate(name, 0)) <= 0)
  44.         {
  45.             if((handl = Fopen(name, 1)) <= 0)
  46.                 return -1;
  47.         }
  48.         bufmode = O_WRONLY;
  49.         break;
  50.  
  51.         case 'a':
  52.         if((handl = Fopen(name, 2)) <= 0)
  53.             return -1;
  54.         Fseek(0L, handl, 2);
  55.         bufmode = O_APONLY;
  56.         break;
  57.  
  58.        default:
  59.         return -1;
  60.     }
  61.  
  62. #ifdef DYNABUF
  63.     MBUFSIZ = BBUFSIZ - 1L;
  64. #endif /* DYNABUF */
  65.  
  66.     bptr = bufr;
  67.     flcount = (-1L);
  68.     return handl;
  69. }
  70.  
  71. stfclose(handl)
  72. int handl;
  73. {
  74.     if(bufmode == O_RDONLY)
  75.         return Fclose(handl);
  76.     if(stflush(handl))
  77.     {
  78.         Fclose(handl);
  79.         return -1;
  80.     }
  81.     return Fclose(handl);
  82. }
  83.  
  84. stputc(c, handl)
  85. unsigned int c;
  86. int handl;
  87. {
  88.     if(flcount >= MBUFSIZ)
  89.     {
  90.         if(Fwrite(handl, (long)BBUFSIZ, bufr) != (long)BBUFSIZ)
  91.             return -1;
  92.         flcount = (-1L);
  93.         bptr  = bufr;
  94.     }
  95.     flcount++;
  96.     *bptr++ = c;
  97.     return 0;
  98. }
  99.  
  100. stgetc(handl)
  101. int handl;
  102. {
  103.     if(flcount <= 0)
  104.     {
  105.         if((flcount = Fread(handl, (long)BBUFSIZ, bufr)) == 0)
  106.             return EOF;
  107.         bptr = bufr;
  108.     }
  109.     flcount--;
  110.     return(*bptr++);
  111. }
  112.  
  113. stflush(handl)
  114. int handl;
  115. {
  116.     if(flcount < 0)
  117.         return 0;
  118.  
  119.     if(Fwrite(handl, (long)(flcount+1L), bufr) != (flcount+1L))
  120.         return -1;
  121.     flcount = (-1L);
  122.     bptr = bufr;
  123.     return 0;
  124. }
  125.         
  126. stfseek(handl, disp, mode)
  127. int handl;
  128. long disp;
  129. int  mode;
  130. {
  131.     if(bufmode != O_RDONLY)
  132.         if(stflush(handl))
  133.             return -1;
  134.     Fseek(disp, handl, mode);
  135.     flcount = (-1L);
  136.     bptr = bufr;
  137.     return 0;
  138. }
  139.  
  140. long stread(handl, b, bytes)
  141. int handl;
  142. unsigned char *b;
  143. long bytes;
  144. {
  145.     long done;
  146.     
  147.     if(bytes == 0L) return 0L;
  148.     
  149.     if(flcount <= 0)
  150.     {
  151.     if((flcount = Fread(handl, (long)BBUFSIZ, bufr)) == 0)
  152.         return 0L;
  153.     bptr = bufr;
  154.     }
  155.     if(flcount >= bytes)
  156.     {
  157.     bcopy(bptr, b, bytes);
  158.     flcount -= bytes;
  159.     bptr += bytes;
  160.     return bytes;
  161.     }
  162.     bcopy(bptr, b, flcount);
  163.     done = flcount;
  164.     flcount = 0;
  165.     return done + stread(handl, b+done, bytes-done);
  166. }
  167.  
  168. #ifndef __GNUC__
  169. void bcopy(s, d, n)
  170. unsigned char *s, *d;
  171. long n;        /* note: long */
  172. {
  173.     while(n--)
  174.     *d++ = *s++;
  175. }
  176. #endif
  177. /* -eof- */
  178.